﻿using System;
using System.Collections.Generic;
using System.Text;

namespace DataStructureSample.ListDs
{
    public class SeqList<T>
        : IListDs<T>
    {
        private T[] data;
        private int count = 0;
        public SeqList(int size)
        {
            data = new T[size];
            count = 0;
        }
        public SeqList()
            : this(10)
        { }
        public T this[int index] => GetEle(index);

        public void Add(T item)
        {
            if (count >= data.Length)
            {
                throw new Exception("");
            }
            data[count] = item;
            count++;
        }

        public void Clear()
        {
            count = 0;
        }

        public void Delete(int index)
        {
            for (int i = index; i < count; i++)
            {
                data[index] = data[index + 1];
            }
            count--;
        }

        public T GetEle(int index)
        {
            if (index >= count)
            {
                return default(T);
            }
            return data[index];
        }

        public int GetLength()
        {
            return count;
        }

        public void Insert(T item, int index)
        {
            for (int i = count - 1; i >= index; i--)
            {
                data[index + 1] = data[index];
            }
            data[index] = item;
            
        }

        public bool IsEmpty()
        {
            return count == 0;
        }

        public int Locate(T item)
        {
            for (int i = 0; i < count; i++)
            {
                if (data[i].Equals(item))
                {
                    return i;
                }
            }
            return -1;
        }
    }
}
